package client.comm.baoding.ui

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.lifecycle.Observer
import client.comm.commlib.toast.toast
import client.comm.baoding.R
import client.comm.baoding.databinding.ActivityWjmmBinding
import client.comm.baoding.ui.vm.WjmmViewModel
import java.util.*
import client.comm.baoding.BaseMiddleActivity

class WjmmActivity : BaseMiddleActivity<WjmmViewModel, ActivityWjmmBinding>() {


    var timerTask: TimerTask? = null
    var timer: Timer? = null
    override fun getLayout(): Int {
        return R.layout.activity_wjmm
    }

    override fun initView() {
        binding?.apply {
            event = this@WjmmActivity
            vm = viewModel

        }
    }

    override fun observerUI() {
        viewModel.isShowPwd.observe(this, Observer {
            if (it) {
                binding.etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding.etPwd.setSelection((binding.etPwd.getText().toString().trim()).length);
            } else {
                binding.etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding.etPwd.setSelection((binding.etPwd.getText().toString().trim()).length);
            }
        })
        viewModel.isShowPwdAgain.observe(this, Observer {
            if (it) {
                binding.etPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding.etPwdAgain.setSelection(
                    (binding.etPwdAgain.getText().toString().trim()).length
                );
            } else {
                binding.etPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding.etPwdAgain.setSelection(
                    (binding.etPwdAgain.getText().toString().trim()).length
                );
            }
        })
        viewModel.codeRet.observe(this, Observer {

            it?.apply {

                if (isSuccess) {

                    toast("获取验证码成功")

                    timerTask?.let { it.cancel() }
                    timer?.let { it.cancel() }

                    viewModel.getCodeEnable.postValue(false)

                    timerTask = object : TimerTask() {
                        var count = 60
                        override fun run() {
                            count--;
                            viewModel.getCodeTxt.postValue("${count}秒后获取")
                            if (count <= 0) {
                                viewModel.getCodeEnable.postValue(true)
                                viewModel.getCodeTxt.postValue("获取验证码")
                                timerTask!!.cancel()

                            }
                        }
                    }
                    timer = Timer().apply { schedule(timerTask, 0, 1000) }

                } else {
                    toast(it.message)
                    viewModel.getCodeEnable.value = true
                    viewModel.getCodeTxt.value = "获取验证码"
                }

            }
        })
        viewModel.okRet.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("找回成功")
                    finish()
                } else {
                    toast(it.message)
                    viewModel.getCodeEnable.value = true
                    viewModel.getCodeTxt.value = "获取验证码"
                }

            }
        })
    }

    override fun onDestroy() {

        timerTask?.let { it.cancel() }
        timer?.let { it.cancel() }

        super.onDestroy()
    }


    override fun initViewModel(): WjmmViewModel = createViewModel()

    fun back() {
        finish()
    }
}